﻿using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using PharmacyPharnet.Core;
using Pharmacy.Data.Entity;

using Pharmacy.Data;
using Pharmacy.Ultilities;
using Pharmacy.Data.Repositories;
using Pharmacy.Business;
using System.Windows.Forms;
using Pharmacy.ExceptionHandling;
using Pharmacy.Ultilities.Resources;
using System.Data;
using Pharmacy.Ultilities.BaseEventArgs;

namespace PharmacyPharnet.MasterData
{
    public partial class FrmMasterItemSelection : DialogViewBase
    {
        #region Declarations
        Repository<MasterItem> lstMasterData;
        IObjectContext pharmacyContext;
        MasterItemBO masterItemBO;
        /// <summary>
        /// Lưu danh sách các sản phẩm đã được chọn trước đó, không hiển thị lên danh sách cho chọn lại.
        /// </summary>
        List<MasterItem> lstSelectedMasterItem;

        /// <summary>
        /// Lưu danh sách ID các sản phẩm đã được chọn trước đó, không hiển thị lên danh sách cho chọn lại.
        /// </summary>
        List<int> lstSelectedMasterItemID;

        public delegate void RowSelectedEventHandler(object sender, RowSelectedArgs e);
        public event RowSelectedEventHandler RowSelected = null;
        #endregion

        #region Constructors
        public FrmMasterItemSelection()
        {
            InitializeComponent();            

        }

        /// <summary>
        /// Danh sách các sản phẩm đã chọn và sẽ không hiển thị lên form để chọn lại.
        /// </summary>
        /// <param name="selectedMasterItem"></param>
        public FrmMasterItemSelection(IObjectContext context)
            : this()
        {
            pharmacyContext = context;

        }

        /// <summary>
        /// Danh sách các sản phẩm đã chọn và sẽ không hiển thị lên form để chọn lại.
        /// </summary>
        /// <param name="selectedMasterItem"></param>
        public FrmMasterItemSelection(IObjectContext context, List<MasterItem> selectedMasterItem)
            :this(context)
        {
            lstSelectedMasterItem = selectedMasterItem; 

        }
        #endregion

        #region Override Methods
        protected override void PopulateData()
        {
            if (lstSelectedMasterItem != null && lstSelectedMasterItem.Count > 0)
            {
                lstSelectedMasterItemID = new List<int>();
                lstSelectedMasterItemID = lstSelectedMasterItem.Select(item => item.ID).ToList();
            }
            BindData();            
        }

        protected override void InitializeBO()
        {
            if (pharmacyContext == null)
                pharmacyContext = ObjectContextFactory.CreatePharmacyContext(Global.UserName, Global.CurrentConnectionString);
            if (masterItemBO == null)
                masterItemBO = new MasterItemBO(pharmacyContext);
            if (lstSelectedMasterItem == null)
                lstSelectedMasterItem = new List<MasterItem>();
                    
        }
        #endregion

        #region Custom Methods
        private void BindData()
        {
            lstMasterData = masterItemBO.ListMasterItem();

            List<MasterItem> lst = null;
            if(lstSelectedMasterItem != null && lstSelectedMasterItem.Count > 0)
                lst = lstMasterData.AsQueryable().Where(item => !lstSelectedMasterItemID.Contains(item.ID)).ToList();
            else
                lst = lstMasterData.AsQueryable().ToList();
            grdMasterItemList.DataSource = null;
            grdMasterItemList.DataSource = lst;
            grvMasterItemList.BestFitColumns();
        }

        private List<MasterItem> GetSelectedItem()
        {
            List<MasterItem> lstMasterItem = grvMasterItemList.DataSource as List<MasterItem>;
            List<MasterItem> lstSelectMasterItem = new List<MasterItem>();
            if (lstMasterItem != null && lstMasterItem.Count > 0)
            {
                foreach (var item in lstMasterItem)
                {
                    if (item.Selected)
                        lstSelectMasterItem.Add(item);
                }
            }
            return lstSelectMasterItem;
        }

        #endregion        

        #region Event Handlers
        private void FrmMasterItem_List_Load(object sender, EventArgs e)
        {
            try
            {
                
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }            
        }
        #endregion                

        private void btnSelectMasterItem_Click(object sender, EventArgs e)
        {
            try
            {
                List<MasterItem> lstSelectMasterItem = GetSelectedItem();
                if (RowSelected != null)
                {
                    RowSelectedArgs args = new RowSelectedArgs();                    
                    args.CurrentListMasterItem = lstSelectMasterItem;
                    RowSelected(sender, args);
                }
                Close();

            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }     
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogWriter.Write(ex.Message, ExceptionHandler.LOG_CAT_GENERAL);
                MessageBox.Show(ex.Message);
            }     
        }
    }
}